<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>创建three.js基础代码</title>
  <script src="../js/three.js"></script>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    body {
      height: 100%;
      overflow: hidden;
    }
  </style>
</head>

<body>

</body>
<script>
  "use strict";
  // 设置场景
  const scene = new THREE.Scene();
  // 设置相机
  const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);
  camera.position.set(-30, 40, 30);
  camera.lookAt(scene.position);

  // 设置绘制对象
  const renderer = new THREE.WebGLRenderer();
  renderer.setClearColor(new THREE.Color(0xEEEEEE));
  renderer.setSize(window.innerWidth, window.innerHeight);
  // 增加阴影
  renderer.shadowMapEnabled = true;

  // 设置辅助测试工具
  const axes = new THREE.AxisHelper(20);
  // 将工具添加到场景中
  scene.add(axes);

  // 设置平面
  const planeGeometry = new THREE.PlaneGeometry(70, 50, 1, 1);
  const planeMaterial = new THREE.MeshLambertMaterial({
    color: 0xcccccc
  });
  const plane = new THREE.Mesh(planeGeometry, planeMaterial);
  plane.rotation.x = -0.5 * Math.PI;
  plane.position.set(15, 0, 0);
  // 打开地面的阴影
  plane.receiveShadow = true;
  scene.add(plane);

  // 设置立方体
  const cubeGeometry = new THREE.CubeGeometry(4, 4, 4);
  const cubeMaterial = new THREE.MeshLambertMaterial({
    color: 0xff0000
  });
  const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
  cube.position.set(-4, 3, 0);
  // 打开立方体的阴影
  plane.castShadow = true;
  scene.add(cube);

  // 设置球面体
  const sphereGeometry = new THREE.SphereGeometry(4, 20, 20); //设置半径和段数
  const sphereMaterial = new THREE.MeshLambertMaterial({
    color: 0xff0000
  });
  const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
  sphere.position.set(20, 4, -2);
  // 打开球面体的阴影
  plane.castShadow = true;
  scene.add(sphere);

  // 将renderer的输出挂在到body中
  document.body.appendChild(renderer.domElement);

  // 设置点光源
  const spotLight = new THREE.SpotLight(0xffffff);
  spotLight.position.set(-40, 60, -10);
  // 定义光源生成阴影
  spotLight.castShadow = true;
  scene.add(spotLight);
  /* 
  只有这两种的材质才会对光源产生反应
    MeshLamberMaterial
    MeshPhongMaterial
    MeshBasicMaterial 是基本材质不会对光有反应的
  */

  // 渲染场景
  renderer.render(scene, camera);
</script>

</html>